package com.lsl.controller;

import com.lsl.bean.UserAccount;
import com.lsl.service.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value = "PageController", description = "用户登录登出接口")
@Controller
public class PageController {
    @Autowired
    private UserAccountService userAccountService;
    @RequestMapping({"/","/index"})
    public String index(HttpServletRequest request, HttpServletResponse response, Model model){
        request.getSession();
        model.addAttribute("msg", "hahaha");
        return "index";
    }
    @RequestMapping("login")
    public ModelAndView login(@RequestParam("loginName") String loginName,
                              @RequestParam("password") String password,
                              ModelAndView mv) {
        // 重定向到到main请求
        mv.setViewName("redirect:/success");
        return mv;
    }
    @PostMapping("login2")
    @ResponseBody
    public ModelAndView Login(HttpServletRequest request
            , @RequestParam("account")String account
            , @RequestParam("pwd")String pwd
            , ModelAndView mv) {
        request.getSession();
        UserAccount user = userAccountService.getUserByAccount(account);
        if (user != null && user.getPwd().equals(pwd) && user.getAccount().equals(account)) {
            request.getSession().setAttribute("useraccount", user);
            mv.setViewName("success");
        }
        return mv;
    }
}
